package com.xiaoshuidi.cloud.module.report.dal.mapper;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.xiaoshuidi.cloud.framework.mybatis.core.mapper.BaseMapperX;
import com.xiaoshuidi.cloud.module.report.controller.workbench.vo.WorkbenchTotalResp;
import com.xiaoshuidi.cloud.module.report.dal.DO.ContractOrderFeesDO;
import com.xiaoshuidi.cloud.module.report.dal.DO.ContractRentorderDO;
import com.xiaoshuidi.cloud.module.report.xxljob.PO.CollectionHandlePO;
import com.xiaoshuidi.cloud.module.report.xxljob.PO.ContractPeriodPO;
import com.xiaoshuidi.cloud.module.report.xxljob.PO.ContractRentorderPO;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@Mapper
@InterceptorIgnore(tenantLine = "true")
public interface ContractRentorderMapper extends BaseMapperX<ContractRentorderDO> {


    ContractRentorderPO selectFirstRenewalRentOrderByContractId(@Param("contractId") Long contractId);

    @MapKey("flow_Type")
    HashMap<Integer,BigDecimal> selectTotalAmountByContractIdGroupByFlowType(@Param("contractId") Long contractId);

    Long selectIdByContractIdOut(@Param("contractId") Long contractId);

    List<ContractPeriodPO> selectContractPeriodByContractIds(@Param("contractId")Long contractId);

    List<Long> queryByContractIdOnPayedInPayTime(@Param("contractId")Long contractId, @Param("start") LocalDateTime start, @Param("end")LocalDateTime end);

    @Select("select * from contract_rentorder " +
            " where (ought_pay_time <= #{end} and ought_pay_time >= #{start}) " +
            " and deleted = 0 and order_type in ('default','custom','meter')")
    List<ContractRentorderDO> selectListByStartAndEnd(@Param("start")LocalDateTime start,@Param("end")LocalDateTime end);

    @Select(" select sum(rent_amount+extras_amount -discount_amount-coupon_discount ) from contract_rentorder " +
            " where deleted = 0 and business_id =#{contractId} and pay_status ='payment ' " +
            " and order_type in ('default','custom','meter') " +
            " and actual_pay_time <= #{end} and actual_pay_time >= #{start} ")
    BigDecimal sumAmountByContractIdAndTime(@Param("contractId")Long contractId,@Param("start")LocalDateTime start,@Param("end")LocalDateTime end);


    @Select(" select sum(coupon_discount ) from contract_rentorder " +
            " where deleted = 0 and business_id =#{contractId} and pay_status ='payment ' " +
            " and order_type in ('default','custom','meter') " +
            " and actual_pay_time <= #{end} and actual_pay_time >= #{start} ")
    BigDecimal sumCouponByContractIdAndTime(@Param("contractId")Long contractId,@Param("start")LocalDateTime start,@Param("end")LocalDateTime end);


    @Select(" select sum(rent_amount+extras_amount -discount_amount-coupon_discount ) from contract_rentorder " +
            " where deleted = 0 and business_id =#{contractId} and pay_status ='payment ' " +
            " and order_type in ('owner','owner_eviction') " +
            " and actual_pay_time <= #{end} and actual_pay_time >= #{start} ")
    BigDecimal sumOwnerAmountByContractIdAndTime(@Param("contractId")Long contractId,@Param("start")LocalDateTime start,@Param("end")LocalDateTime end);


    @Select("select sum(rent_amount+extras_amount -discount_amount-coupon_discount ) from contract_rentorder " +
            " where deleted = 0 and business_id =#{contractId} and pay_status ='payment' " +
            " and order_type in ('default','custom') and actual_pay_time <= #{startTime}" )
    BigDecimal sumAllAmountByContractId(@Param("contractId")Long contractId,@Param("startTime")LocalDateTime startTime);

    @Select("select sum(coupon_discount ) from contract_rentorder " +
            " where deleted = 0 and business_id =#{contractId} and pay_status ='payment' " +
            " and order_type in ('default','custom') and actual_pay_time <= #{startTime}" )
    BigDecimal sumAllCouponByContractId(@Param("contractId")Long contractId,@Param("startTime")LocalDateTime startTime);


    @Select("select sum(rent_amount -discount_amount-coupon_discount) from contract_rentorder " +
            " where deleted = 0 and business_id =#{contractId} and pay_status ='payment' " +
            " and order_type ='owner' and actual_pay_time < #{startTime}" )
    BigDecimal sumOwnerAllAmountByContractId(@Param("contractId")Long contractId,@Param("startTime")LocalDateTime startTime);

    @Select("select sum(paid_rent_utilities) from contract_rentorder " +
            " where deleted = 0 and business_id = #{contractId} and order_type in ('default','custom','meter') and (ought_pay_time <= #{end} and ought_pay_time >=#{start} ) ")
    BigDecimal selectTotalReceivableInCurrentMonth(@Param("contractId")Long contractId,@Param("start")LocalDateTime start,@Param("end")LocalDateTime end);

    @Select("select sum(paid_rent_utilities) from contract_rentorder " +
            " where deleted = 0 and business_id = #{contractId} and order_type in ('default','custom','meter') and pay_status = 'payment'  and (ought_pay_time <= #{end} and ought_pay_time >=#{start}  ) ")
    BigDecimal selectTotalReceivedInCurrentMonth(@Param("contractId")Long contractId,@Param("start")LocalDateTime start,@Param("end")LocalDateTime end);

    @Select("select sum(paid_rent_utilities) from contract_rentorder " +
            " where deleted = 0 and business_id = #{contractId}  and order_type in ('default','custom','meter') and pay_status = 'unpaid'  and (ought_pay_time  <= #{end} and ought_pay_time >=#{start}  ) ")
    BigDecimal selectTotalNotReceivedInCurrentMonth(@Param("contractId")Long contractId,@Param("start")LocalDateTime start,@Param("end")LocalDateTime end);


    @Select("select sum(paid_rent_utilities) from contract_rentorder " +
            " where deleted = 0 and business_id = #{contractId} and order_type in ('default','custom','meter') and pay_status = 'payment'  and ought_pay_time >= NOW()  ")
    BigDecimal selectCurAdvance(@Param("contractId")Long contractId);

    @Select("select sum(deposit_amount) from contract_rentorder " +
            " where deleted = 0 and business_id = #{contractId} and order_type in ('default','custom','meter') and pay_status = 'payment'  ")
    BigDecimal sumPayedTotalDepositByContractId (@Param("contractId")Long contractId);


    @Select(" select end_time from contract_rentorder " +
            " where deleted = 0 and business_id = #{contractId} and order_type in ('default','custom','meter') and pay_status = 'payment' order by actual_pay_time desc limit 1 ")
    LocalDate selectRecentPayedEndTime (@Param("contractId")Long contractId);

    @Select(" select * from contract_rentorder " +
            " where deleted = 0 and business_id = #{contractId} and order_type in ('default','custom','meter') and start_time <= " +
            "#{date} and end_time >= #{date} limit 1")
    ContractRentorderDO selectRentOrderByContractIdAndDate(@Param("contractId") Long contractId,
                                                           @Param("date") LocalDate date);

    @Select("select sum(rent_amount-discount_amount-coupon_discount) from contract_rentorder " +
            " where deleted = 0 and business_id = #{contractId} " +
            " and order_type = 'default' and actual_pay_time <= #{end}")
    BigDecimal selectTotalRentByContractIdAndPayed(@Param("contractId") Long contractId,
                                                   @Param("end") LocalDateTime end);

    @Select("SELECT IFNULL( sum( CASE order_type WHEN 'default' THEN rent_amount - discount_amount - coupon_discount " +
            "ELSE paid_rent_utilities END ), 0 )  from contract_rentorder " +
            "where business_id = #{contractId} " +
            "AND order_type  IN ('default','custom')" +
            " and actual_pay_time <= #{end} AND end_time <=  (SELECT end_time FROM " +
            "contract_rentorder WHERE id = #{rentOrderId})")
    BigDecimal selectTotalRentByContractIdAndOrderId(@Param("contractId") Long contractId,
                                                     @Param("end") LocalDateTime end,
                                                     @Param("rentOrderId") Long rentOrderId);

    WorkbenchTotalResp selectTodayTotalFeeAmount(@Param("tenantId") Long tenantId,
                                                 @Param("apartmentId") Long apartmentId);

    BigDecimal selectNotReceivedTotal(@Param("tenantId") Long tenantId, @Param("apartmentId") Long apartmentId);

    List<Long> queryByContractIdOnPayed(@Param("contractId") Long contractId,
                                        @Param("startTime") LocalDateTime startTime);

    List<Long> queryContractIdsCurrentMonthPayed(@Param("contractId") Long contractId,
                                                 @Param("start") LocalDateTime start, @Param("end") LocalDateTime end);

    List<Long> selectOwnerRenewalOrderIds(@Param("list") List<Long> renewalIds);

    List<ContractOrderFeesDO> selectOwnerDipositList(@Param("contractIds") List<Long> contractIds,
                                                     @Param("orderType") String orderType);
    Long countListByParam(CollectionHandlePO collectionHandlePO);
    
    List<ContractRentorderDO> selectListByParam(CollectionHandlePO collectionHandlePO);
}
